package org.example.ecommerce_system.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.ecommerce_system.entity.Product;
import org.example.ecommerce_system.entity.User;
import org.example.ecommerce_system.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class UserController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("/login")
    public User login(@RequestBody User user){
        return userMapper.login(user);
    }

    @RequestMapping("/getAllUser")
    public PageInfo<User> getAllUser(@RequestParam(defaultValue = "1") int currentPage,
                                     @RequestParam(defaultValue = "5") int pageSize){
        try {
            PageHelper.startPage(currentPage, pageSize);
            List<User> users = userMapper.getAllUser();
            return new PageInfo<>(users);
        } finally {
            PageHelper.clearPage(); // 确保清除分页设置
        }
    }

    @RequestMapping("/searchUser")
    public PageInfo<User> searchUser(@RequestParam String userName, @RequestParam(defaultValue = "1") int currentPage,
                                           @RequestParam(defaultValue = "5") int pageSize){
        try {
            PageHelper.startPage(currentPage, pageSize);
            List<User> users = userMapper.searchUser(userName);
            return new PageInfo<>(users);
        } finally {
            PageHelper.clearPage();
        }
    }

    @RequestMapping("/deleteUser")
    public int deleteUser(int userID){
        return userMapper.deleteUser(userID);
    }

    @RequestMapping("/insertUser")
    public int insertUser(@RequestBody User user){
        return userMapper.insertUser(user);
    }

    @RequestMapping("/updatetUser")
    public int updateUser(@RequestBody User user){
        return userMapper.updateUser(user);
    }
}
